var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    actId:'',
    openId:'',
    orderId: '',
    initUsers:[
      { nickName: 'Wayne Rooney',     avatarUrl:'https://fcyx.xyzbaby.cn/uploadeFile/laqeelin/userImg/1.jpg'},
      { nickName: '余前煌', avatarUrl: 'https://fcyx.xyzbaby.cn/uploadeFile/laqeelin/userImg/2.jpg' },
      { nickName: '凌龙润雨', avatarUrl: 'https://fcyx.xyzbaby.cn/uploadeFile/laqeelin/userImg/3.jpg' },
      { nickName: '阳光', avatarUrl: 'https://fcyx.xyzbaby.cn/uploadeFile/laqeelin/userImg/4.jpg' },
      { nickName: 'YY', avatarUrl: 'https://fcyx.xyzbaby.cn/uploadeFile/laqeelin/userImg/5.jpg' },
      { nickName: 'Allen 阿南', avatarUrl: 'https://fcyx.xyzbaby.cn/uploadeFile/laqeelin/userImg/6.jpg' },
      { nickName: '海岸@黑色', avatarUrl: 'https://fcyx.xyzbaby.cn/uploadeFile/laqeelin/userImg/7.jpg' },
      { nickName: '向希', avatarUrl: 'https://fcyx.xyzbaby.cn/uploadeFile/laqeelin/userImg/8.jpg' },
      { nickName: '那又如何', avatarUrl: 'https://fcyx.xyzbaby.cn/uploadeFile/laqeelin/userImg/9.jpg' },
      {
        nickName: 'miliqs', avatarUrl: 'https://fcyx.xyzbaby.cn/uploadeFile/laqeelin/userImg/10.jpg' },
    ],
    act:'',
    imgs:[],
    userInfo: '',
    help: true,//是否可以助力
    isHelp: false,//是否助力成功
    isShare: 0,//是否是分享人
    disModel: 0,
    animation: '',
    helpUsers:[],
    helps:[],
    show: false,//是否显示漂浮名单
    showUser: {},
    scene:'',
    interval:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    if (options.id) {
      _this.setData({
        actId: options.id,
      })
    }
    if (options.openId) {
      _this.setData({
        openId: options.openId,
      })
    }
    console.log("options",options);
    if (options.scene){
      var scene = decodeURIComponent(options.scene)
      _this.setData({
        scene: scene,
      })
    
   }
    console.log('scene:' + options.scene);
  },
  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    var _this = this;
    if (this.data.interval) {
      clearInterval(this.data.interval);
    }
    var pages = "../active/active$id%" + _this.data.actId + "&openId%" + _this.data.openId;
    if (_this.data.scene) {
      pages = "../active/active$scene%" + _this.data.scene ;
    }

    //检测用户是否登录
    util.userLogin(function () {
      //用户登录成功后回调
      wx.getStorage({
        key: 'userInfo',
        success: function (res) {
          _this.setData({ userInfo: res.data });
          if (_this.data.scene) {
            _this.getCodeInfo(_this.data.scene)
          } else {
            _this.init();
          }
        }
      })

    }, pages);
   
  },
  /**
  * 初始化数据
  */
  init: function () {
    var _this = this;
    _this.findHelps();
    //如果是自己查看自己分享的
    if (_this.data.userInfo.openId != _this.data.openId) {
      console.log('助力人')
      _this.setData({ help: false, isShare: 2 });
    } else {
      console.log('分享人')
      _this.setData({ help: true, isShare: 1 });
    }
 
    _this.findInfo();
  },
  /**
   * 查询活动详情
   */
  findInfo: function () {
    var _this = this;
    util.request('/wxapi/helpActivity/queryShare/' + _this.data.actId + '/' + _this.data.openId, {},        function (data) {
      //如果已助力完成 
      var share = data.rows[0]
      console.log(share)
      if (share.orderId){
        _this.setData({orderId: share.orderId });
      }
      if (_this.data.isShare==1){//是否是分享人
        //1 分享人
        if (share.shaStatus == '2' ) {//状态为已完成，显示领取
          _this.setData({ help: true, disModel: 3 });
        } else if (share.shaStatus == '3') {//状态为已领取，显示查看
          _this.setData({ help: true, disModel: 4});
        }else{
          _this.setData({disModel: 1 });
        }
      }else{
        //2 助力人，则显示已完成
        if (share.shaStatus != '1' ) {
          _this.setData({ help: true, disModel: 2, });
        }else{
          _this.setData({ disModel: 1 });
        }
      }
     
      var helps = data.rows[0].helps
      _this.setData({helps: helps });
      //查询自己是否已在助力名单中，已助力则显示领取
      if (helps){
        for (let i in helps) {
          if (helps[i].userId == _this.data.userInfo.openId) {
            _this.setData({ help: true, disModel: 4, helps: helps});
          }
        }
      }
     
      _this.setData({
        act: data.rows[0].activity,
        imgs: data.rows[0].activity.hacImgs.split(","),
      })
   
    }, true);
  },
  

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var _this = this;
    
    return {
      title: '我在免费领取' + _this.data.act.hacName + '邀你来助力',
      path: 'pages/active/active?id=' + _this.data.act.actId + '&openId=' + _this.data.userInfo.openId,
      imageUrl: _this.data.act.hacRemark,
      success: function (res) {
        // 转发成功
        _this.shareSuccess();

      },
    }
  },
  /**
  * 分享成功
  */
  shareSuccess: function () {
    var _this = this;
    util.request('/wxapi/helpActivity/addShare/' + _this.data.act.actId, {}, function (data) {
      
    }, true);
  },
  /**
  * 助力
  */
  formSubmit: function (e){
    console.log('领取礼品：', e)
    var formId = e.detail.formId;
    var _this = this;
    util.request('/wxapi/helpActivity/addHelp/' + _this.data.actId + '/' + _this.data.openId + '/' +        formId, {},  function (data) {
      console.log(data);
       
      _this.setData({ help: true, isHelp: true, orderId: data.rows[0].orderId});
    }, true);

  },
  /**
  * 查看礼品
  */
  showPrize: function () {
  
    var _this = this;
    _this.setData({ isHelp: false });
    wx.navigateTo({
      url: '../customerBenefit/benefit?id=' + _this.data.orderId,
      success: function () { },        //成功后的回调；
    })
  },
  /**
  * 领取礼品
  */
  recPrize: function () {
    var _this = this;
    _this.setData({ isHelp: false });
    _this.setData({ help: true, isShare: 1 });
    wx.navigateTo({
      url: '../getGift/getGift?id=' + _this.data.actId + "&openId=" + _this.data.openId,
      success: function () { },        //成功后的回调；
    })
  },

  /**
  * 查询助力用户
  */
  findHelps: function () {
    var _this = this;
    util.request('/wxapi/helpActivity/selectHelps',{}, function (data) {
      var users=[]
      if (data.rows){
        var num = data.rows.length;
        var users = data.rows;



        if (num<10){
          for(let i=0;i<10-num;i++){
            users.push(_this.data.initUsers[i]);
          }
        }
      }else{
        users = _this.data.initUsers;
      }
      _this.setData({ helpUsers: users});
      var  c=0;
      var index=0;
      var interval = setInterval(function () {
        //循环执行代码
        index=_this.carousel(c, index);
        c++;
      }, 3000) //循环时间 这里是1秒 
      
      _this.setData({ interval: interval });
    }, true);
  },
  /**
   * 
   */
  carousel: function (c, index) {
    var _this = this;
    if (c % 2 == 0){
      _this.setData({ showUser: _this.data.helpUsers[index] });
      _this.setData({ show: true });
      index++;
    } else if (c % 2 == 1){
      _this.setData({ show: false });
    } 
    if (index > 9) {
      index = 0;
    }
    return index;
  },

/**
 * 查询二维码信息
 */
  getCodeInfo: function (id) {
    var _this = this;
    util.request('/wxapi/helpActivity/getCodeInfo/' + id, {}, function (data) {
      console.log(data)
     
      if (data) {
        _this.setData({
          actId: data.rows[0].actId,
          openId: data.rows[0].userId,
        })
      }
      _this.init();
    }, true);
  },
})
